/**
 * Created with IntelliJ IDEA.
 * Description:
 */
class ListNode {
     int val;
    ListNode next;
     ListNode() {}
     ListNode(int val) { this.val = val; }
    ListNode(int val, ListNode next) { this.val = val; this.next = next; }
}

public class Solution {
    public ListNode removeElements(ListNode head, int val) {
        if(head == null){
            return null;
        }
        ListNode pos = head;
        ListNode cur = head.next;

        while(cur != null){
            if(cur.val == val){
                pos.next = cur.next;
                cur = cur.next;
            }else{
                pos = cur;
                cur = cur.next;
            }
        }

        if(head.val == val){
            head = head.next;
        }
        return head;
    }
}